package com.eascs.web.p2p.member.untils;

import java.util.regex.Pattern;

import org.apache.commons.lang3.StringUtils;

public final class EmailUtils {
	/**
	 * 邮件格式正则
	 */
    private static final Pattern EMAIL_PATTERN = Pattern.compile("^([0-9a-zA-Z]([-.\\w]*[0-9a-zA-Z])*@([0-9a-zA-Z][-\\w]*[0-9a-zA-Z]\\.)+[a-zA-Z]{2,9})$");
    
    /**
     * 验证邮箱格式是否正确
     * @param email
     * @return
     */
    public static boolean validateEmail(String email) {
        if (StringUtils.isBlank(email)) {
            return false;
        }
        return EMAIL_PATTERN.matcher(email).matches();
    }
    
    /**
     * 邮箱信息屏蔽
     * @param email
     * @return
     */
    public static String closedEmail(String email) {
        if (!validateEmail(email)) {
            return email;
        }
        
        String[] resultAry = email.split("@");
        String prefixStr = resultAry[0];
        StringBuilder returnResult = new StringBuilder();
        
        if(prefixStr.length() == 1) {
        	return email;
        } else if(prefixStr.length() == 2) {
        	return returnResult.append(prefixStr.substring(0,1)).append("*@").append(resultAry[1]).toString();
        } else if(prefixStr.length() > 2 && prefixStr.length() <= 4) {
        	return returnResult.append(prefixStr.substring(0,prefixStr.length() - 2)).append("**@").append(resultAry[1]).toString();
        } else {
            return returnResult.append(prefixStr.substring(0,prefixStr.length() - 4)).append("****@").append(resultAry[1]).toString();
        }
    }
    
}
